package com.jyuxuan.recruitment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jyuxuan.recruitment.mapper.EnterpriseLoginMapper;
import com.jyuxuan.recruitment.mapper.EnterpriseMapper;
import com.jyuxuan.recruitment.pojo.Enterprise;
import com.jyuxuan.recruitment.pojo.EnterpriseLogin;
import com.jyuxuan.recruitment.utils.DataMap;
import com.jyuxuan.recruitment.utils.RouterUtil;
import com.jyuxuan.recruitment.utils.oss.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class EnterpriseServiceImpl {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private EnterpriseLoginMapper enterpriseLoginMapper;

    /**
     * 查看企业信息
     *
     * @param number
     * @return
     */
    public DataMap enterpriseView(String number) {
        Enterprise enterprise = enterpriseMapper.findByNumber(number);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("enterprise", enterprise);
        return DataMap.success().setData(jsonObject);
    }

    /**
     * 填写企业信息
     *
     * @param enterprise
     * @return
     */
    public DataMap enterpriseFill(Enterprise enterprise) {
        int n = enterpriseMapper.update(enterprise);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }

    /**
     * 修改企业信息
     *
     * @param enterprise
     * @return
     */
    public DataMap enterpriseModify(Enterprise enterprise) {
        int n = enterpriseMapper.update(enterprise);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }

    /**
     * 上传营业执照
     *
     * @param file
     * @param number
     * @return
     */
    public DataMap uploadLicense(MultipartFile file, String number) {
        OSSUtil ossClient = new OSSUtil();
        // 先删除之前的图片
        Enterprise enterprise = enterpriseMapper.findByNumber(number);
        String license_delete = enterprise.getLicense();
        if (license_delete != null) {
            String[] ss = license_delete.split("/");
            int length = ss.length;
            ossClient.deleteImg(ss[length - 1]);
        }
        // 保存
        String fileName = number + "-" + RouterUtil.generateRandomString() + ".jpg";
        int m = 0;
        try {
            if (!(file).isEmpty()) {
                //将文件上传
                String name = ossClient.uploadImg2Oss(file, fileName);
                //获取文件的URl地址以便前台显示
                String license = ossClient.getImgUrl(name);
                enterprise.setLicense(license);
                enterprise.setState(0);
                m = enterpriseMapper.update(enterprise);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return DataMap.fail();
        }
        if (m > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }


    /**
     * 企业注册
     *
     * @param enterpriseLogin
     * @return
     */
    public DataMap enterpriseRegister(EnterpriseLogin enterpriseLogin) {
        int m = enterpriseLoginMapper.save(enterpriseLogin);
        Enterprise enterprise = new Enterprise(enterpriseLogin.getNumber());
        int n = enterpriseMapper.save(enterprise);
        if (m > 0 && n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }

    /**
     * 查询企业信息-管理员
     *
     * @param enterprise
     * @return
     */
    public DataMap enterpriseList(Enterprise enterprise) {
        List<Enterprise> enterprises = enterpriseMapper.findDynamic(enterprise);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("enterprises", enterprises);
        return DataMap.success().setData(jsonObject);
    }

    /**
     * 审核企业信息
     *
     * @param enterpriseId
     * @return
     */
    public DataMap enterpriseAudit(int enterpriseId, int state) {
        Enterprise enterprise = enterpriseMapper.findByEnterpriseid(enterpriseId);
        enterprise.setState(state);
        int n = enterpriseMapper.update(enterprise);
        if (n > 0) {
            return DataMap.success();
        } else {
            return DataMap.fail();
        }
    }


}
